package com.google.code.sysarch.v2.v2_final;

import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashMap;

/** 
 * This singleton class handles input.
 * @author Fabian Nicolay 
 */
public final class InputManager implements KeyListener, MouseListener, MouseMotionListener {
	
	/** singleton instance of InputManager */
	private static InputManager singleton = new InputManager();
	/** Stores the state of all the keys on the keyboard */
	private HashMap<Integer,Boolean> keys = new HashMap<Integer,Boolean>();
	/** Stores the state of up to 3 mouse buttons */
	private boolean[] mouseButtons = new boolean[3];
	private int mouse_x, mouse_y;
	
	/** 
	 * Prevents from creating multiple instances of InputManager. 
	 * The get method must be used to get a reference to the single instance.
	 */
	private InputManager() { }
	
	/** 
	 * Returns the singleton instance of InputManager
	 * @return the singleton instance of InputManager
	 */
	public static InputManager get() {
		return singleton;
	} 
	
	/**
	 * Checks if the key with the given vk-keycode is pressed or not
	 * 
	 * @param keycode the keycode of the key to check
	 * @return true if the key is pressed
	 * @see java.awt.event.KeyEvent
	 */
	public boolean isKeyPressed(int keycode) {
		if (keys.get(keycode)==null) {
			keys.put(keycode, false);
		}
		return keys.get(keycode);
	}

	/**
	 * Checks if the mousekey with the given keycode is pressed or not
	 * 
	 * @param keycode the keycode of the key to check
	 * @return true if the key is pressed
	 * @see java.awt.event.MouseEvent
	 */
	public boolean isMousePressed(int button) {
		return mouseButtons[button];
	}
	
	/**
	 * @return the current mouse x position
	 */
	public int getMouseX() {
		return mouse_x;
	}

	/**
	 * @return the current mouse y position
	 */
	public int getMouseY() {
		return mouse_y;
	}

	public void keyPressed(KeyEvent e) {
		keys.put(e.getKeyCode(), true);
	}

	public void keyReleased(KeyEvent e) {
		keys.put(e.getKeyCode(), false);
	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
	}
	
	public void mousePressed(MouseEvent e) {
		switch (e.getButton()) {
			case MouseEvent.BUTTON1:
				mouseButtons[0] = true;
				break;
			case MouseEvent.BUTTON2:
				mouseButtons[1] = true;
				break;
			case MouseEvent.BUTTON3:
				mouseButtons[2] = true;
				break;
			case MouseEvent.NOBUTTON:
			default:				
		}
	}	
	
	public void mouseReleased(MouseEvent e) {
		switch (e.getButton()) {
			case MouseEvent.BUTTON1:
				mouseButtons[0] = false;
				break;
			case MouseEvent.BUTTON2:
				mouseButtons[1] = false;
				break;
			case MouseEvent.BUTTON3:
				mouseButtons[2] = false;
				break;
			case MouseEvent.NOBUTTON:
			default:			
		}
	}	
	
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub		
	}	
	
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub		
	}	
	
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub		
	}
	
	public void mouseMoved(MouseEvent e) {
		mouse_x = e.getX();
		mouse_y = e.getY();		
	}
	
	public void mouseDragged(MouseEvent e) {
		mouse_x = e.getX();
		mouse_y = e.getY();		
	}	

}
